package com.hmy.contract.manager.worklfow;

import cn.hutool.json.JSONUtil;
import com.hmy.ccp.common.result.FeignResult;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.workflow.api.bo.request.task.ApprovalTaskRequest;
import com.hmy.workflow.api.bo.request.task.QueryNextActiveTaskRequest;
import com.hmy.workflow.api.bo.response.task.ApprovalTaskResponse;
import com.hmy.workflow.api.client.task.FlowTaskClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

@Component
@Slf4j
public class FlowTaskClientManager {

    @Resource
    private FlowTaskClient flowTaskClient;

    /**
     * 审批流程
     *
     * @param approvalTaskRequest approvalTaskRequest
     * @return {@link ApprovalTaskResponse}
     */
    public ApprovalTaskResponse complete(ApprovalTaskRequest approvalTaskRequest) {
        log.info("FlowTaskClientRpc.complete params:{}", JSONUtil.toJsonPrettyStr(approvalTaskRequest));
        FeignResult<ApprovalTaskResponse> response = null;
        response = flowTaskClient.complete(approvalTaskRequest);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000006);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("FlowTaskClientRpc.complete response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }

    /**
     * 查询下一个任务节点的id
     *
     * @param queryNextTaskRequest 查询下一个任务节点的请求
     * @return taskId 个节点的任务id
     */
    public String queryNextActiveTask(QueryNextActiveTaskRequest queryNextTaskRequest) {
        log.info("FlowTaskClientRpc.queryNextActiveTask params:{}", queryNextTaskRequest);
        FeignResult<String> response = flowTaskClient.queryNextActiveTask(queryNextTaskRequest);
        if (response.isFailed()) {
            log.error("FlowTaskClientRpc.queryNextActiveTask fail");
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("FlowTaskClientRpc.queryNextActiveTask response:{}", response);
        return response.getData();
    }


}